#include "../server/net/TcpServer.h"
#include "../server/net/EventLoop.h"
#include "./http/HttpServer.h"
#include <lua.hpp>
using namespace yhaida;

void onRequest(const HttpRequest &req, HttpResponse *resp)
{
    LOG_INFO<<"Headers "<<req.methodString()<<" "<<req.path();
    
    if(req.path()=="/log")
    {
        resp->setStatusCode(HttpResponse::k200Ok);
        resp->setStatusQuery("OK");
        resp->setContentType("text/html");
        // std::string nowTime = Timestamp::now().toFormattedString();
        std::string text=readFile("../html/shudu_request.html");
        resp->setBody(text);
    }
}

int main(int argc, char *argv[])
{   
    lua_State* lua = luaL_newstate();
    if(luaL_dofile(lua,"../config.lua"))
    {
        abort();
    }
    lua_getglobal(lua,"server_port");
    lua_getglobal(lua,"server_numberThread");
    int server_port = lua_tonumber(lua,-2);
    int server_numberThread = lua_tonumber(lua,-1);
    lua_pop(lua,2);

    Logging::setLogLevel(Logging::TRACE);
    EventLoop loop;
    InetAddress listenAddr(server_port);
    HttpServer server(&loop, listenAddr,TcpServer::kReusePort);
    server.setHttpCallback(onRequest);
    if(server_numberThread>1)
    {
        server.setThreadNum(server_numberThread);
    }
    server.start();
    loop.loop();
}